<template>
  <div class='tinymce'>
    <h4 style="margin:0 0 20px 0">
      <el-row>
        <el-col :span="19">
          <span v-if="dataPush">修改推送</span>
          <span v-else>添加推送</span>
        </el-col>
        <el-col :span="5" style="text-align:right">
          <i class="el-icon-error" style="background:#e6e6e6" @click="goBack"></i>
        </el-col>
      </el-row>
    </h4>
    <div>
      
     <el-form  label-width="80px" :rules="aling" ref="formAling" :model="formLabelAlign">
        <el-form-item label="推送标题" prop="title">
          <el-input v-model="formLabelAlign.title"></el-input>
        </el-form-item>
        <el-form-item label="发布时间" prop="push_time">
          <div>
            <el-radio-group v-model="formLabelAlign.is_timer" @change="changTime">
              <el-radio  label="0">立即发布</el-radio>
              <el-radio label="1">定时发布</el-radio>
            </el-radio-group>
          </div>
          <el-date-picker
            v-model="formLabelAlign.push_time"
            type="datetime"
            :disabled="timeStatus"
            value-format="timestamp"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="推送内容" prop="tiny">
          <el-input type="textarea"  v-model='tinymceHtml' :rows="2" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item>
           <div style="padding-left:20px;">
              <el-button type="success" @click="submitArticle('formAling')">提交推送</el-button>
            </div>
        </el-form-item>
      </el-form>
    
    </div>
  </div>
</template>

<script>
import {mapActions,mapMutations,mapGetters,mapState} from 'vuex'
import Http from '@/Http/Http'

export default {
  name: 'tinymce',
  props:['dataPush'],
  data () {
    const title = (rule,value,callback) => {
      if (value === '') {
        callback(new Error('请输入标题'))
      } else {
        callback()
      }
    }
    const tiny = (rule,value,callback) => {
      if (value === '') {
        callback(new Error('请输入推送内容'))
      } else {
        callback()
      }
    }
    
    return {
      postData:{
        token:'',
      },
      timeStatus:false,
      dialogImageUrl: '',
      dialogVisible: false,
      
      formLabelAlign:{
        title:'',
        is_timer:'1',
        push_time:''
      },
      aling:{
        title:[
          {validator:title,trigger:'blur'}
        ],
        tiny:[
          {validator:tiny,trigger:'blur'}
        ]
      },

      imgstuts:false, //富文本编辑器部分
      tinymceHtml:'',
      fileList:[],

    }
    
  },

  mounted () {
    
  },
  created() {
    this.getToken()
  },
  methods:{
    getToken() {
      Http.QiniuToken({})
      .then( res => {
        this.postData.token = res.data.data.uptoken;
        this.tokenimgUrl = res.data.data.domain
      })
      .catch( error => {
        this.$router.push('/')
        
      });
      if (this.dataPush) {
        this.formLabelAlign.title = this.dataPush.title;
        this.formLabelAlign.is_timer = this.dataPush.is_timer;
        this.formLabelAlign.push_time = this.dataPush.push_time;
        this.tinymceHtml = this.dataPush.content;
        this.formLabelAlign.is_timer = '1';
      }
     
    },
    changTime(val) {
      if (val == 0){
        this.timeStatus = true;
        this.formLabelAlign.push_time = Math.round(new Date())
      } else {
        this.timeStatus = false;
      }
    },
    handleRemove(file, fileList) {
      
    },
  
   
   
    submitArticle(val) {
      
      this.$refs[val].validate((valid) => {
        if (valid) {
          if (this.formLabelAlign.is_timer == 1 && this.formLabelAlign.push_time == '') {
            this.$message.error('请选择发布时间');
            return
          };
          if(this.tinymceHtml ==''){
            this.$message.error('请输入推送内容');
            return
          }
          const param = {
            "title":this.formLabelAlign.title,  //文章标题
            "is_timer":this.formLabelAlign.is_timer,  //上线状态 
            "content":this.tinymceHtml, // 文章内容  实体列表 
            "push_time":this.formLabelAlign.push_time,
          }
          this.dataPush ? this.EditPushFun(param) : this.addAPushFun(param)
        }
      })
      
    },
    addAPushFun(param) {
      Http.addPush(param)
        .then( res => {
          if( res.data.code == 200 ){
            this.$message.success('推送发布成功')
            this.goBack()         
          } else if(res.data.code == -1003) {
            this.$message.error(`请求失败原因：${res.data.message}`)
            this.$router.push('/')
          }
        })
        .catch( error => {
          this.$message.error('服务器请求失败')
        })
    },
    EditPushFun(param) {
      param.id = this.dataPush.id;
      Http.editPush(param)
      .then( res => {
        if( res.data.code == 200 ){
          this.$message.success('推送修改成功')
          this.goBack()         
        } else if(res.data.code == -1003) {
          this.$message.error(`请求失败原因：${res.data.message}`)
          this.$router.push('/')
        }
      })
      .catch( error => {
        this.$message.error('服务器请求失败')
      })
    },
    goBack(){
      this.$emit('getlist')
    },
    
    //富文本编辑器部分
    upImageFun(){
      this.imgstuts = true
    },
    handleOkContent(responed){
      this.fileList.push(`http://${this.tokenimgUrl}/${responed.key}`)
    },
    backFun(){
      this.imgstuts = false;
      this.fileList =[]
      this.$refs.upload.clearFiles(); 
    },
    apendHtml(){
      console.dir(this.fileList)
      let arrHtml='';
      this.fileList.forEach(item => {
        arrHtml += `<img style="width:20%" src="${item}" alt=""/>`;
      })
      this.tinymceHtml = `${this.tinymceHtml}${arrHtml}`;
      this.imgstuts = false
      this.fileList =[]
      this.$refs.upload.clearFiles(); 
    }
  },
 
}
</script>
<style>
.el-textarea__inner{
  height:146px
}
.initbox{
  position: relative;
}
.initBtn{
  position: absolute;
  opacity: 0;
  position: absolute;
  z-index: 2;
  top: 36px;
  left: 786px;
  width: 30px;
  padding: 0;
  height: 33px;
  border: 1px solid red;
}

#tinymce_ifr{
  height:500px !important
}
</style>
